package com.gin.retrofit.utils

import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule

/**
 * @author BX-Gin
 * @since 2024/3/9 17:16
 */
class JacksonUtils {

    companion object {
        @JvmStatic
        val MAPPER: ObjectMapper = ObjectMapper()
            //美化输出
            .enable(SerializationFeature.INDENT_OUTPUT)
            .enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)
            .enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
            //true - 遇到没有的属性就报错 false - 没有的属性不会管，不会报错
            .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
            // 支持 ZonedDateTime
            .registerModule(JavaTimeModule())

        /**
         * 打印对象
         * @param obj Any
         */
        @JvmStatic
        fun println(obj: Any?) {
            if (obj == null)
                println("NULL")
            else
                kotlin.io.println(MAPPER.writeValueAsString(obj))
        }
    }
}